/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.startup.musicstore.services.Impl;

import com.startup.musicstore.model.Album;
import com.startup.musicstore.model.AlbumCategory;
import com.startup.musicstore.services.CategoryMostSales;
import com.startup.musicstore.services.crud.AlbumCategoryCrudService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author linda
 */
@Service("categoryMostSales")
public class CategoryMostSalesImpl implements CategoryMostSales{
    @Autowired
    private AlbumCategoryCrudService albumCategoryCrudService;

    String key = "";
    

    @Override
    public Map<String, Integer> getMostSalesPerAlbumCategory() {
     //this is for the albumCategories array
        List<AlbumCategory> albumCategories = albumCategoryCrudService.findAll();
        //To create a array for the most sales
        Map<String, Integer> mostSalesMade = new HashMap<String, Integer>();
        Map<String, Integer> salesOnCategory = new HashMap<String, Integer>();
        //To create a list of just the category names
        List<String> existingCategories = new ArrayList<String>();        
        
        Integer mostSales = 0;
        
        for (AlbumCategory cat : albumCategories) {
            //create a list of the albums in a specific category
            List<Album> albums = cat.getAlbums();
            //if the List existing Categories does not contain a specific category name
            //then add that name to the list
            if (!existingCategories.contains(cat.getCategoryName())) {
                //adding the name
                existingCategories.add(cat.getCategoryName());
                for (Album album : albums) {
                    //noe tun through the albums in that specific category
                    //place the category name and the amount of sales in the 
                    //salesOncategory HashMap array. 
                    salesOnCategory.put(cat.getCategoryName(), mostSales += album.getSales());                    
                }
            }
            //set most sales to zero again
            mostSales = 0;
        }
        
        mostSales = categoryWithMostSales(salesOnCategory, existingCategories, mostSales);
        categoryWithSameAsMostSale(salesOnCategory, existingCategories, mostSales, mostSalesMade);
        
        //now return the populate Map array with the categories that has the same value
        // as most sales
        return mostSalesMade;
    }
    
    //getting the value of the most sales
    private Integer categoryWithMostSales(Map<String, Integer> salesOnCategory, List<String> existingCategories, Integer mostSales) {
        //now go through the HashMap array with the categories and their sales
        for (int i = 0; i < salesOnCategory.size(); i++) {
            //then get a key i.e. catgeory's name as you have a list with all the catgories names
            //you can use that
            for (int j = 0; j < existingCategories.size(); j++) {
                //start with the first category place the name as the key
                key = existingCategories.get(j);
                //will compare the first value to all the values in the array
                //then then the second value etc. Thus it has the value of i
                mostSales = salesOnCategory.get(existingCategories.get(i));
                //now check if the value is greater than any of the other values
                //in the array
                if (mostSales > salesOnCategory.get(key)) {
                    mostSales = salesOnCategory.get(key);
                }
            }
        }
        //return the value of the most sales
        return mostSales;
    }  
    
    //getting all the categories that has the same value as the most sales
    private void categoryWithSameAsMostSale(Map<String, Integer> salesOnCategory, List<String> existingCategories, Integer mostSales, Map<String, Integer> mostSalesMade) {
        //same calculation as with the previous one except now checking if it 
        //is equal in stead of greater than. 
        for (int i = 0; i < salesOnCategory.size(); i++) {
            for (int j = 0; j < existingCategories.size(); j++) {
                key = existingCategories.get(j);
                if (mostSales == salesOnCategory.get(key)) {
                    //if it is equal it is placed in the most salesMade Map Array 
                    mostSalesMade.put(key, salesOnCategory.get(key));
                }
            }
        }
    }
    
    
    
}
